package nciae.db.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import nciae.db.mapper.*;
import nciae.db.pojo.entity.*;
import nciae.db.service.ExportService;
import nciae.db.service.ProjectService;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.io.OutputStream;

@Service
public class ExportServiceImpl implements ExportService {

    @Autowired
    private DocSurveyMapper docSurveyMapper;

    @Autowired
    private DocSchemeMapper docSchemeMapper;

    @Autowired
    private DocReportMapper docReportMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public void generateSurveyWord(Long id, OutputStream outputStream) throws IOException {

        QueryWrapper<DocSurvey> docSurveyQueryWrapper = new QueryWrapper<>();
        docSurveyQueryWrapper.eq("id", id);
        DocSurvey docSurvey = docSurveyMapper.selectOne(docSurveyQueryWrapper);

        if (docSurvey == null){
            return;
        }

        QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
        projectQueryWrapper.eq("id", docSurvey.getProjectId());
        Project project = projectMapper.selectOne(projectQueryWrapper);

        // Mock 数据，可以根据ID从数据库获取实际数据
        String title = project.getProjectName() + "诊断问卷";
        String TmpType = "";
        if(docSurvey.getDocType() == 0 ){
            TmpType = "问卷";
        }
        if(docSurvey.getDocType() == 1 ){
            TmpType = "诊断提纲";
        }
        if(docSurvey.getDocType() == 2 ){
            TmpType = "诊断任务单";
        }
        String[] contentLines = {
                "二级学院名称：" + docSurvey.getDepartment(),
                "诊断方式：" + TmpType,
                "诊断内容：" + docSurvey.getContent(),
                "文档版本：" + docSurvey.getVersion()
        };

        XWPFDocument document = new XWPFDocument();

        // 标题
        XWPFParagraph titleParagraph = document.createParagraph();
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun titleRun = titleParagraph.createRun();
        titleRun.setText(title);
        titleRun.setFontFamily("宋体");
        titleRun.setFontSize(16);
        titleRun.setBold(true);

        // 正文
        XWPFParagraph contentParagraph = document.createParagraph();
        contentParagraph.setSpacingBetween(1.5); // 行间距
        XWPFRun contentRun = contentParagraph.createRun();

        for (String line : contentLines) {
            contentRun.setText(line);
            contentRun.addBreak(); // 添加换行
        }

        // 保存文档
        document.write(outputStream);
        document.close();
    }


    @Override
    public void generateSurveyPdf(Long id, OutputStream outputStream) {
        try {
            QueryWrapper<DocSurvey> docSurveyQueryWrapper = new QueryWrapper<>();
            docSurveyQueryWrapper.eq("id", id);
            DocSurvey docSurvey = docSurveyMapper.selectOne(docSurveyQueryWrapper);
            if (docSurvey == null){
                return;
            }
            QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
            projectQueryWrapper.eq("id", docSurvey.getProjectId());
            Project project = projectMapper.selectOne(projectQueryWrapper);


            // Mock 数据，可以根据ID从数据库获取实际数据
            String title = project.getProjectName() + "诊断问卷";
            String TmpType = "";
            if(docSurvey.getDocType() == 0 ){
                TmpType = "问卷";
            }
            if(docSurvey.getDocType() == 1 ){
                TmpType = "诊断提纲";
            }
            if(docSurvey.getDocType() == 2 ){
                TmpType = "诊断任务单";
            }
            String content = "二级学院名称：" + docSurvey.getDepartment() + "\n" + "诊断方式：" + TmpType + "\n" + "诊断内容：" + docSurvey.getContent() + "\n" + "文档版本：" + docSurvey.getVersion();


            // 创建文档实例
            Document document = new Document();

            // 使用 PdfWriter 将文档绑定到输出流
            PdfWriter.getInstance(document, outputStream);

            // 打开文档
            document.open();

            // 加载中文字体 (宋体)
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

            // 标题字体
            Font titleFont = new Font(baseFont, 16, Font.BOLD);
            // 正文字体
            Font contentFont = new Font(baseFont, 12, Font.NORMAL);

            // 标题
            Paragraph titleParagraph = new Paragraph(title, titleFont);
            titleParagraph.setAlignment(Element.ALIGN_CENTER); // 居中对齐

            // 正文
            Paragraph contentParagraph = new Paragraph(content, contentFont);
            contentParagraph.setLeading(18f); // 设置行间距

            // 添加到文档
            document.add(titleParagraph);
            document.add(Chunk.NEWLINE); // 空行
            document.add(contentParagraph);

            // 关闭文档
            document.close();

            System.out.println("PDF 生成成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void generateSchemeWord(Long id, ServletOutputStream outputStream) throws IOException {
        QueryWrapper<DocScheme> docSchemeQueryWrapper = new QueryWrapper<>();
        docSchemeQueryWrapper.eq("id", id);
        DocScheme docScheme = docSchemeMapper.selectOne(docSchemeQueryWrapper);
        if (docScheme == null) {
            return;
        }

        QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
        projectQueryWrapper.eq("id", docScheme.getProjectId());
        Project project = projectMapper.selectOne(projectQueryWrapper);

        // Mock 数据，可以根据ID从数据库获取实际数据
        String title = project.getProjectName() + "诊断方案";
        String[] contentLines = {
                "二级单位：" + docScheme.getDepartment(),
                "诊断背景：" + docScheme.getBackground(),
                "诊断目的：" + docScheme.getTarget(),
                "诊断区域与对象：" + docScheme.getObject(),
                "诊断内容：" + docScheme.getContent(),
                "诊断方法：" + docScheme.getMethod(),
                "诊断流程：" + docScheme.getFlow(),
                "预期诊断成果：" + docScheme.getResult(),
                "人员组织分工：" + docScheme.getPerson(),
                "日程安排：" + docScheme.getSchedule(),
                "文档版本：" + docScheme.getVersion()
        };

        XWPFDocument document = new XWPFDocument();

        // 标题
        XWPFParagraph titleParagraph = document.createParagraph();
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun titleRun = titleParagraph.createRun();
        titleRun.setText(title);
        titleRun.setFontFamily("宋体");
        titleRun.setFontSize(16);
        titleRun.setBold(true);

        // 正文
        XWPFParagraph contentParagraph = document.createParagraph();
        contentParagraph.setSpacingBetween(1.5); // 行间距
        XWPFRun contentRun = contentParagraph.createRun();

        for (String line : contentLines) {
            contentRun.setText(line);
            contentRun.addBreak(); // 添加换行
        }

        // 保存文档
        document.write(outputStream);
        document.close();
    }

    @Override
    public void generateSchemePdf(Long id, ServletOutputStream outputStream) {
        try {
            QueryWrapper<DocScheme> docSchemeQueryWrapper = new QueryWrapper<>();
            docSchemeQueryWrapper.eq("id", id);
            DocScheme docScheme = docSchemeMapper.selectOne(docSchemeQueryWrapper);
            if(docScheme == null){
                return;
            }
            QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
            projectQueryWrapper.eq("id", docScheme.getProjectId());
            Project project = projectMapper.selectOne(projectQueryWrapper);


            // Mock 数据，可以根据ID从数据库获取实际数据
            String title = project.getProjectName() + "诊断方案";
            String content = "二级单位：" + docScheme.getDepartment() + "\n" + "诊断背景：" + docScheme.getBackground() + "\n" +
                    "诊断目的：" + docScheme.getTarget() + "\n" + "诊断区域与对象：" + docScheme.getObject() + "\n" + "诊断内容：" + docScheme.getContent() + "\n" +
                    "诊断方法：" + docScheme.getMethod() + "\n" + "诊断流程：" + docScheme.getFlow() + "\n" + "预期诊断成果：" + docScheme.getResult() + "\n" +
                    "人员组织分工：" + docScheme.getPerson() + "\n" + "日程安排：" + docScheme.getSchedule() + "\n" + "文档版本：" + docScheme.getVersion() + "\n";

            // 创建文档实例
            Document document = new Document();

            // 使用 PdfWriter 将文档绑定到输出流
            PdfWriter.getInstance(document, outputStream);

            // 打开文档
            document.open();

            // 加载中文字体 (宋体)
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

            // 标题字体
            Font titleFont = new Font(baseFont, 16, Font.BOLD);
            // 正文字体
            Font contentFont = new Font(baseFont, 12, Font.NORMAL);

            // 标题
            Paragraph titleParagraph = new Paragraph(title, titleFont);
            titleParagraph.setAlignment(Element.ALIGN_CENTER); // 居中对齐

            // 正文
            Paragraph contentParagraph = new Paragraph(content, contentFont);
            contentParagraph.setLeading(18f); // 设置行间距

            // 添加到文档
            document.add(titleParagraph);
            document.add(Chunk.NEWLINE); // 空行
            document.add(contentParagraph);

            // 关闭文档
            document.close();

            System.out.println("PDF 生成成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void generateReportWord(Long id, ServletOutputStream outputStream) throws IOException {
        QueryWrapper<DocReport> docReportQueryWrapper = new QueryWrapper<>();
        docReportQueryWrapper.eq("id", id);
        DocReport docReport = docReportMapper.selectOne(docReportQueryWrapper);
        if (docReport == null) {
            return;
        }
        QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
        projectQueryWrapper.eq("id", docReport.getProjectId());
        Project project = projectMapper.selectOne(projectQueryWrapper);

        // Mock 数据，可以根据ID从数据库获取实际数据
        String title = project.getProjectName() + "诊断报告";
        String[] contentLines = {
                "二级单位：" + docReport.getDepartment(),
                "诊断背景：" + docReport.getBackground(),
                "诊断目的：" + docReport.getTarget(),
                "调研诊断分析结果：" + docReport.getResult(),
                "思考与建议：" + docReport.getSuggest(),
                "文档版本：" + docReport.getVersion()
        };

        XWPFDocument document = new XWPFDocument();

        // 标题
        XWPFParagraph titleParagraph = document.createParagraph();
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun titleRun = titleParagraph.createRun();
        titleRun.setText(title);
        titleRun.setFontFamily("宋体");
        titleRun.setFontSize(16);
        titleRun.setBold(true);

        // 正文
        XWPFParagraph contentParagraph = document.createParagraph();
        contentParagraph.setSpacingBetween(1.5); // 行间距
        XWPFRun contentRun = contentParagraph.createRun();

        for (String line : contentLines) {
            contentRun.setText(line);
            contentRun.addBreak(); // 添加换行
        }

        // 保存文档
        document.write(outputStream);
        document.close();
    }


    @Override
    public void generateReportPdf(Long id, ServletOutputStream outputStream) {
        try {
            QueryWrapper<DocReport> docReportQueryWrapper = new QueryWrapper<>();
            docReportQueryWrapper.eq("id", id);
            DocReport docReport = docReportMapper.selectOne(docReportQueryWrapper);
            if(docReport == null){
                return;
            }
            QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
            projectQueryWrapper.eq("id", docReport.getProjectId());
            Project project = projectMapper.selectOne(projectQueryWrapper);


            // Mock 数据，可以根据ID从数据库获取实际数据
            String title = project.getProjectName() + "诊断报告";
            String content = "二级单位：" + docReport.getDepartment() + "\n" + "诊断背景：" + docReport.getBackground() + "\n" +
                    "诊断目的：" + docReport.getTarget() + "\n" + "调研诊断分析结果：" + docReport.getResult() + "\n" + "思考与建议：" + docReport.getSuggest() + "\n" +
                    "文档版本：" + docReport.getVersion() + "\n";

            // 创建文档实例
            Document document = new Document();

            // 使用 PdfWriter 将文档绑定到输出流
            PdfWriter.getInstance(document, outputStream);

            // 打开文档
            document.open();

            // 加载中文字体 (宋体)
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

            // 标题字体
            Font titleFont = new Font(baseFont, 16, Font.BOLD);
            // 正文字体
            Font contentFont = new Font(baseFont, 12, Font.NORMAL);

            // 标题
            Paragraph titleParagraph = new Paragraph(title, titleFont);
            titleParagraph.setAlignment(Element.ALIGN_CENTER); // 居中对齐

            // 正文
            Paragraph contentParagraph = new Paragraph(content, contentFont);
            contentParagraph.setLeading(18f); // 设置行间距

            // 添加到文档
            document.add(titleParagraph);
            document.add(Chunk.NEWLINE); // 空行
            document.add(contentParagraph);

            // 关闭文档
            document.close();

            System.out.println("PDF 生成成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void exportWorldSurveyInfo(Long version, ServletOutputStream outputStream) throws IOException {
        QueryWrapper<DocSurvey> docSurveyQueryWrapper = new QueryWrapper<>();
        docSurveyQueryWrapper.eq("version", version);
        DocSurvey docSurvey = docSurveyMapper.selectOne(docSurveyQueryWrapper);

        if (docSurvey == null){
            return;
        }

        QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
        projectQueryWrapper.eq("id", docSurvey.getProjectId());
        Project project = projectMapper.selectOne(projectQueryWrapper);

        // Mock 数据，可以根据ID从数据库获取实际数据
        String title = project.getProjectName() + "诊断问卷";
        String TmpType = "";
        if(docSurvey.getDocType() == 0 ){
            TmpType = "问卷";
        }
        if(docSurvey.getDocType() == 1 ){
            TmpType = "诊断提纲";
        }
        if(docSurvey.getDocType() == 2 ){
            TmpType = "诊断任务单";
        }
        Integer subUserId = docSurvey.getUserId();
        String Submituser = "";
        if(subUserId != null){
            User user = userMapper.selectById(subUserId);
            Submituser = user.getRealName();
        }
        Integer confirmId = docSurvey.getConfirmId();
        String ConfirmUser = "无";
        String stateStr = "暂未审批";
        String ConfirmDate = "暂未审批";
        String Advice = "暂未审批";
        if (confirmId != null){
            User user = userMapper.selectById(confirmId);
            ConfirmUser = user.getRealName();
            Integer state = docSurvey.getState();
            if(state == 0){
                stateStr = "已提交";
            }else if(state == -1){
                stateStr = "退回修改";
            }else if(state == 1){
                stateStr = "通过";
            }
            ConfirmDate = DateUtil.format(docSurvey.getConfirmDate(),"yyyy-MM-dd");
        }
        String[] contentLines = {
                "二级学院名称：" + docSurvey.getDepartment(),
                "诊断方式：" + TmpType,
                "诊断内容：" + docSurvey.getContent(),
                "文档版本：" + docSurvey.getVersion(),
                "提交日期：" + DateUtil.format(docSurvey.getSubmitDate(),"yyyy-MM-dd"),
                "提交用户：" + Submituser,
                "审批状态：" + stateStr,
                "审批意见：" + Advice,
                "审批时间：" + ConfirmDate,
                "审批人：" + ConfirmUser
        };

        XWPFDocument document = new XWPFDocument();

        // 标题
        XWPFParagraph titleParagraph = document.createParagraph();
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun titleRun = titleParagraph.createRun();
        titleRun.setText(title);
        titleRun.setFontFamily("宋体");
        titleRun.setFontSize(16);
        titleRun.setBold(true);

        // 正文
        XWPFParagraph contentParagraph = document.createParagraph();
        contentParagraph.setSpacingBetween(1.5); // 行间距
        XWPFRun contentRun = contentParagraph.createRun();

        for (String line : contentLines) {
            contentRun.setText(line);
            contentRun.addBreak(); // 添加换行
        }

        // 保存文档
        document.write(outputStream);
        document.close();
    }

    @Override
    public void exportPdfSurveyInfo(Long version, ServletOutputStream outputStream) {
        try {
            QueryWrapper<DocSurvey> docSurveyQueryWrapper = new QueryWrapper<>();
            docSurveyQueryWrapper.eq("version", version);
            DocSurvey docSurvey = docSurveyMapper.selectOne(docSurveyQueryWrapper);
            if (docSurvey == null){
                return;
            }
            QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
            projectQueryWrapper.eq("id", docSurvey.getProjectId());
            Project project = projectMapper.selectOne(projectQueryWrapper);


            // Mock 数据，可以根据ID从数据库获取实际数据
            String title = project.getProjectName() + "诊断问卷";
            String TmpType = "";
            if(docSurvey.getDocType() == 0 ){
                TmpType = "问卷";
            }
            if(docSurvey.getDocType() == 1 ){
                TmpType = "诊断提纲";
            }
            if(docSurvey.getDocType() == 2 ){
                TmpType = "诊断任务单";
            }
            Integer subUserId = docSurvey.getUserId();
            String Submituser = "";
            if(subUserId != null){
                User user = userMapper.selectById(subUserId);
                Submituser = user.getRealName();
            }
            Integer confirmId = docSurvey.getConfirmId();
            String ConfirmUser = "无";
            String stateStr = "暂未审批";
            String ConfirmDate = "暂未审批";
            String Advice = "暂未审批";
            if (confirmId != null){
                User user = userMapper.selectById(confirmId);
                ConfirmUser = user.getRealName();
                Integer state = docSurvey.getState();
                if(state == 0){
                    stateStr = "已提交";
                }else if(state == -1){
                    stateStr = "退回修改";
                }else if(state == 1){
                    stateStr = "通过";
                }
                ConfirmDate = DateUtil.format(docSurvey.getConfirmDate(),"yyyy-MM-dd");
            }
            String content =
                    "二级学院名称：" + docSurvey.getDepartment() + "\n" +
                            "诊断方式：" + TmpType + "\n" +
                            "诊断内容：" + docSurvey.getContent() + "\n" +
                            "文档版本：" + docSurvey.getVersion() + "\n" +
                            "提交日期：" + DateUtil.format(docSurvey.getSubmitDate(),"yyyy-MM-dd") + "\n" +
                            "提交用户：" + Submituser + "\n" +
                            "审批状态：" + stateStr + "\n" +
                            "审批意见：" + Advice + "\n" +
                            "审批时间：" + ConfirmDate + "\n" +
                            "审批人：" + ConfirmUser;
            // 创建文档实例
            Document document = new Document();

            // 使用 PdfWriter 将文档绑定到输出流
            PdfWriter.getInstance(document, outputStream);

            // 打开文档
            document.open();

            // 加载中文字体 (宋体)
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

            // 标题字体
            Font titleFont = new Font(baseFont, 16, Font.BOLD);
            // 正文字体
            Font contentFont = new Font(baseFont, 12, Font.NORMAL);

            // 标题
            Paragraph titleParagraph = new Paragraph(title, titleFont);
            titleParagraph.setAlignment(Element.ALIGN_CENTER); // 居中对齐

            // 正文
            Paragraph contentParagraph = new Paragraph(content, contentFont);
            contentParagraph.setLeading(18f); // 设置行间距

            // 添加到文档
            document.add(titleParagraph);
            document.add(Chunk.NEWLINE); // 空行
            document.add(contentParagraph);

            // 关闭文档
            document.close();

            System.out.println("PDF 生成成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void exportPdfSchemaInfo(Long version, ServletOutputStream outputStream) {
        try {
            QueryWrapper<DocScheme> docSchemeQueryWrapper = new QueryWrapper<>();
            docSchemeQueryWrapper.eq("version", version);
            DocScheme docScheme = docSchemeMapper.selectOne(docSchemeQueryWrapper);
            if(docScheme == null){
                return;
            }
            QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
            projectQueryWrapper.eq("id", docScheme.getProjectId());
            Project project = projectMapper.selectOne(projectQueryWrapper);

            Integer SubmitUserId = docScheme.getUserId();
            String SubmitUser = "未提交";
            if(SubmitUserId != null){
                User user = userMapper.selectById(SubmitUserId);
                SubmitUser = user.getRealName();
            }
            Integer ConfirmId = docScheme.getConfirmId();
            String ConfirmUser = "暂未审批";
            String ConfirmState = "暂未审批";
            String ConfirmDate = "暂未审批";
            String Advice = "暂未审批";
            if(ConfirmId != null){
                User user = userMapper.selectById(ConfirmId);
                ConfirmUser = user.getRealName();
                Integer state = docScheme.getState();
                if(state == 0){
                    ConfirmState = "修改";
                }else if(state == 1){
                    ConfirmState = "通过";
                }else if(state == 2){
                    ConfirmState = "未提交";
                }
                ConfirmDate = DateUtil.format(docScheme.getConfirmDate(),"yyyy-MM-dd");
            }

            // Mock 数据，可以根据ID从数据库获取实际数据
            String title = project.getProjectName() + "诊断方案";
            String content = "二级单位：" + docScheme.getDepartment() + "\n" + "诊断背景：" + docScheme.getBackground() + "\n" +
                    "诊断目的：" + docScheme.getTarget() + "\n" + "诊断区域与对象：" + docScheme.getObject() + "\n" + "诊断内容：" + docScheme.getContent() + "\n" +
                    "诊断方法：" + docScheme.getMethod() + "\n" + "诊断流程：" + docScheme.getFlow() + "\n" + "预期诊断成果：" + docScheme.getResult() + "\n" +
                    "人员组织分工：" + docScheme.getPerson() + "\n" + "日程安排：" + docScheme.getSchedule() + "\n" + "文档版本：" + docScheme.getVersion() + "\n" +
                    "提交日期：" + DateUtil.format(docScheme.getSubmitDate(),"yyyy-MM-dd") + "\n" +  "提交用户：" + SubmitUser + "\n" + "审批状态：" + ConfirmState + "\n" + "审批意见：" + Advice + "\n" +
                    "审批时间：" + ConfirmDate + "\n" + "审批人：" + ConfirmUser;


            // 创建文档实例
            Document document = new Document();

            // 使用 PdfWriter 将文档绑定到输出流
            PdfWriter.getInstance(document, outputStream);

            // 打开文档
            document.open();

            // 加载中文字体 (宋体)
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

            // 标题字体
            Font titleFont = new Font(baseFont, 16, Font.BOLD);
            // 正文字体
            Font contentFont = new Font(baseFont, 12, Font.NORMAL);

            // 标题
            Paragraph titleParagraph = new Paragraph(title, titleFont);
            titleParagraph.setAlignment(Element.ALIGN_CENTER); // 居中对齐

            // 正文
            Paragraph contentParagraph = new Paragraph(content, contentFont);
            contentParagraph.setLeading(18f); // 设置行间距

            // 添加到文档
            document.add(titleParagraph);
            document.add(Chunk.NEWLINE); // 空行
            document.add(contentParagraph);

            // 关闭文档
            document.close();

            System.out.println("PDF 生成成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void exportWorldSchemaInfo(Long version, ServletOutputStream outputStream) throws IOException {
        QueryWrapper<DocScheme> docSchemeQueryWrapper = new QueryWrapper<>();
        docSchemeQueryWrapper.eq("version", version);
        DocScheme docScheme = docSchemeMapper.selectOne(docSchemeQueryWrapper);
        if (docScheme == null) {
            return;
        }

        QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
        projectQueryWrapper.eq("id", docScheme.getProjectId());
        Project project = projectMapper.selectOne(projectQueryWrapper);

        Integer SubmitUserId = docScheme.getUserId();
        String SubmitUser = "未提交";
        if(SubmitUserId != null){
            User user = userMapper.selectById(SubmitUserId);
            SubmitUser = user.getRealName();
        }

        Integer ConfirmId = docScheme.getConfirmId();
        String ConfirmUser = "暂未审批";
        String ConfirmState = "暂未审批";
        String ConfirmDate = "暂未审批";
        String Advice = "暂未审批";
        if(ConfirmId != null){
            User user = userMapper.selectById(ConfirmId);
            ConfirmUser = user.getRealName();
            Integer state = docScheme.getState();
            if(state == 0){
                ConfirmState = "修改";
            }else if(state == 1){
                ConfirmState = "通过";
            }else if(state == 2){
                ConfirmState = "未提交";
            }
            ConfirmDate = DateUtil.format(docScheme.getConfirmDate(),"yyyy-MM-dd");
        }
        // Mock 数据，可以根据ID从数据库获取实际数据
        String title = project.getProjectName() + "诊断方案";
        String[] contentLines = {
                "二级单位：" + docScheme.getDepartment(),
                "诊断背景：" + docScheme.getBackground(),
                "诊断目的：" + docScheme.getTarget(),
                "诊断区域与对象：" + docScheme.getObject(),
                "诊断内容：" + docScheme.getContent(),
                "诊断方法：" + docScheme.getMethod(),
                "诊断流程：" + docScheme.getFlow(),
                "预期诊断成果：" + docScheme.getResult(),
                "人员组织分工：" + docScheme.getPerson(),
                "日程安排：" + docScheme.getSchedule(),
                "文档版本：" + docScheme.getVersion(),
                "提交日期：" + DateUtil.format(docScheme.getSubmitDate(),"yyyy-MM-dd"),
                "提交用户：" + SubmitUser,
                "审批状态：" + ConfirmState,
                "审批意见：" + Advice,
                "审批时间：" + ConfirmDate,
                "审批人：" + ConfirmUser

        };

        XWPFDocument document = new XWPFDocument();

        // 标题
        XWPFParagraph titleParagraph = document.createParagraph();
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun titleRun = titleParagraph.createRun();
        titleRun.setText(title);
        titleRun.setFontFamily("宋体");
        titleRun.setFontSize(16);
        titleRun.setBold(true);

        // 正文
        XWPFParagraph contentParagraph = document.createParagraph();
        contentParagraph.setSpacingBetween(1.5); // 行间距
        XWPFRun contentRun = contentParagraph.createRun();

        for (String line : contentLines) {
            contentRun.setText(line);
            contentRun.addBreak(); // 添加换行
        }

        // 保存文档
        document.write(outputStream);
        document.close();
    }

    @Override
    public void exportPdfReportInfo(Long version, ServletOutputStream outputStream) {
        try {
            QueryWrapper<DocReport> docReportQueryWrapper = new QueryWrapper<>();
            docReportQueryWrapper.eq("version", version);
            DocReport docReport = docReportMapper.selectOne(docReportQueryWrapper);
            if(docReport == null){
                return;
            }
            QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
            projectQueryWrapper.eq("id", docReport.getProjectId());
            Project project = projectMapper.selectOne(projectQueryWrapper);

            String SubmitUser = "未提交";
            String SubmitDate = "未提交";
            Integer SubmitUserId = docReport.getUserId();
            if(SubmitUserId != null){
                User user = userMapper.selectById(SubmitUserId);
                SubmitUser = user.getRealName();
                SubmitDate = DateUtil.format(docReport.getSubmitDate(),"yyyy-MM-dd");
            }

            Integer ConfirmId = docReport.getConfirmId();
            String ConfirmUser = "暂未审批";
            String ConfirmState = "暂未审批";
            String ConfirmDate = "暂未审批";
            String Advice = "暂未审批";
            if(ConfirmId != null){
                User user = userMapper.selectById(ConfirmId);
                ConfirmUser = user.getRealName();
                if(docReport.getState() == 0){
                    ConfirmState = "修改";
                }else if(docReport.getState() == 1){
                    ConfirmState = "通过";
                }
                ConfirmDate = DateUtil.format(docReport.getConfirmDate(),"yyyy-MM-dd");
                Advice = docReport.getAdvice();

            }
            // Mock 数据，可以根据ID从数据库获取实际数据
            String title = project.getProjectName() + "诊断报告";
            String content = "二级单位：" + docReport.getDepartment() + "\n" + "诊断背景：" + docReport.getBackground() + "\n" +
                    "诊断目的：" + docReport.getTarget() + "\n" + "调研诊断分析结果：" + docReport.getResult() + "\n" + "思考与建议：" + docReport.getSuggest() + "\n" +
                    "文档版本：" + docReport.getVersion() + "\n" + "提交日期：" + SubmitDate + "\n" + "提交用户：" + SubmitUser + "\n" +
                    "审批状态：" + ConfirmState + "\n" + "审批意见：" + Advice + "\n" + "审批时间：" + ConfirmDate + "\n" + "审批人：" + ConfirmUser;

            // 创建文档实例
            Document document = new Document();

            // 使用 PdfWriter 将文档绑定到输出流
            PdfWriter.getInstance(document, outputStream);

            // 打开文档
            document.open();

            // 加载中文字体 (宋体)
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

            // 标题字体
            Font titleFont = new Font(baseFont, 16, Font.BOLD);
            // 正文字体
            Font contentFont = new Font(baseFont, 12, Font.NORMAL);

            // 标题
            Paragraph titleParagraph = new Paragraph(title, titleFont);
            titleParagraph.setAlignment(Element.ALIGN_CENTER); // 居中对齐

            // 正文
            Paragraph contentParagraph = new Paragraph(content, contentFont);
            contentParagraph.setLeading(18f); // 设置行间距

            // 添加到文档
            document.add(titleParagraph);
            document.add(Chunk.NEWLINE); // 空行
            document.add(contentParagraph);

            // 关闭文档
            document.close();

            System.out.println("PDF 生成成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void exportWorldReportInfo(Long version, ServletOutputStream outputStream) throws IOException {
        QueryWrapper<DocReport> docReportQueryWrapper = new QueryWrapper<>();
        docReportQueryWrapper.eq("version", version);
        DocReport docReport = docReportMapper.selectOne(docReportQueryWrapper);
        if (docReport == null) {
            return;
        }
        QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
        projectQueryWrapper.eq("id", docReport.getProjectId());
        Project project = projectMapper.selectOne(projectQueryWrapper);

        String SubmitUser = "未提交";
        String SubmitDate = "未提交";
        Integer SubmitUserId = docReport.getUserId();
        if(SubmitUserId != null){
            User user = userMapper.selectById(SubmitUserId);
            SubmitUser = user.getRealName();
            SubmitDate = DateUtil.format(docReport.getSubmitDate(),"yyyy-MM-dd");
        }
        Integer ConfirmId = docReport.getConfirmId();
        String ConfirmUser = "暂未审批";
        String ConfirmState = "暂未审批";
        String ConfirmDate = "暂未审批";
        String Advice = "暂未审批";
        if(ConfirmId != null){
            User user = userMapper.selectById(ConfirmId);
            ConfirmUser = user.getRealName();
            if(docReport.getState() == 0){
                ConfirmState = "修改";
            }else if(docReport.getState() == 1){
                ConfirmState = "通过";
            }
            ConfirmDate = DateUtil.format(docReport.getConfirmDate(),"yyyy-MM-dd");
            Advice = docReport.getAdvice();

        }
        // Mock 数据，可以根据ID从数据库获取实际数据
        String title = project.getProjectName() + "诊断报告";
        String[] contentLines = {
                "二级单位：" + docReport.getDepartment(),
                "诊断背景：" + docReport.getBackground(),
                "诊断目的：" + docReport.getTarget(),
                "调研诊断分析结果：" + docReport.getResult(),
                "思考与建议：" + docReport.getSuggest(),
                "文档版本：" + docReport.getVersion(),
                "提交日期：" + SubmitDate,
                "提交用户：" + SubmitUser,
                "审批状态：" + ConfirmState,
                "审批意见：" + Advice,
                "审批时间：" + ConfirmDate,
                "审批人：" + ConfirmUser
        };

        XWPFDocument document = new XWPFDocument();

        // 标题
        XWPFParagraph titleParagraph = document.createParagraph();
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun titleRun = titleParagraph.createRun();
        titleRun.setText(title);
        titleRun.setFontFamily("宋体");
        titleRun.setFontSize(16);
        titleRun.setBold(true);

        // 正文
        XWPFParagraph contentParagraph = document.createParagraph();
        contentParagraph.setSpacingBetween(1.5); // 行间距
        XWPFRun contentRun = contentParagraph.createRun();

        for (String line : contentLines) {
            contentRun.setText(line);
            contentRun.addBreak(); // 添加换行
        }

        // 保存文档
        document.write(outputStream);
        document.close();
    }
}
